<script type="text/javascript">

function makePOSTRequest(friends) {
  var params = {};
  var json = new Array();
  friends.each(function(friend){
  	var tmp = {'name':friend.getDisplayName(),
  	           'id':friend.getId()+"@myspace"};
  	json.push(tmp);
  	});
  var postdata = {'msg': 'ok',
  				  'items': gadgets.json.stringify(json)};
  console.debug(postdata);
  params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.POST;
  // params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.JSON;  
  params[gadgets.io.RequestParameters.POST_DATA] = gadgets.io.encodeValues(postdata);
  var url = "http://reachlin.appspot.com/stuff99/profile/main";
  gadgets.io.makeRequest(url, response, params);
};

function response(obj) {
      //obj.text contains the text of the page that was requested
      console.debug(obj);
      var str = obj.text;
      var html =  str;  
      document.getElementById('message').innerHTML = html;
};

/**
 * Request for friend information.
 */
 function getData() {    
   var req = opensocial.newDataRequest();
   req.add(req.newFetchPersonRequest(opensocial.DataRequest.PersonId.VIEWER), 'viewer');
   req.add(req.newFetchPeopleRequest(opensocial.DataRequest.Group.VIEWER_FRIENDS), 'viewerFriends');
   req.send(onLoadFriends);
 };

var viewer = null;

/**
 * Parses the response to the friend information request and generates
 * html to list the friends along with their display name.
 *
 * @param {Object} dataResponse Friend information that was requested.
 */
 function onLoadFriends(dataResponse) {
 	 viewer = dataResponse.get('viewer').getData();
     var viewerFriends = dataResponse.get('viewerFriends').getData();
     makePOSTRequest(viewerFriends);
 };
 
 function add(link) {
 	window.open("http://reachlin.appspot.com/stuff99/profile/add?person="+viewer.getId(),"添加新宝贝");
 };

 gadgets.util.registerOnLoadHandler(getData);

 </script>
 <div><h2><font color="blue">我的99个宝贝</font></h2></div>
 <div><a href="#" onClick="add(this)">添加新宝贝</a></div>
 <div id="message"> </div>
 <div><a href="http://reachlin.appspot.com">版权所有</a></div>